//
//  HYBroadCastController.m
//  XPlayer
//
//  Created by 张水 on 2023/10/25.
//

#import "HYFaceToFaceController.h"
#import "HYSettingHeadView.h"
#import "HYFaceToFaceCell.h"
#import "HYHomeVideoListCell.h"
#import "HYFaceToFaceViewModel.h"
#import "HYBroadCastController.h"
#import "HYHomeFolderListCell.h"
#import "HYVideoRoomViewController.h"
#import "HYWKWebViewController.h"
#import "HYPreviewImageController.h"
#import "HYAudioRoomViewController.h"

@interface HYFaceToFaceController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) HYFaceToFaceViewModel *viewModel;

@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, strong) NSArray *dataSource;

@end

@implementation HYFaceToFaceController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = NSLocalizedString(@"面对面传送文件", nil);
    self.view.backgroundColor = AppBgThemeColor;
    self.navigationBarView.navagationBarStyle = Left_right_button_show;
    self.navigationBarView.rightLabel.text = NSLocalizedString(@"搜索", nil);
    [self configUI];
    [self loadData];
    [self bindSignal];
}
- (void)rightButtonClick
{
    [self jumpBroadController];
}
- (void)jumpBroadController
{
    @weakify(self);
    HYBroadCastController *castVc = [[HYBroadCastController alloc]init];
    castVc.transfer = ^(HYHomeFileItemViewModel *videoModel) {
        @strongify(self);
        [self.viewModel.updatedContentSignal  sendNext:videoModel];
    };
    castVc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:castVc animated:YES completion:nil];
}
- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.refreshSubJect subscribeNext:^(id x) {
        @strongify(self);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }];
    [self.viewModel.jumpSubJect subscribeNext:^(id x) {
        @strongify(self);
        [self jumpBroadController];
    }];
    
    [self.viewModel.playSubject subscribeNext:^(HYHomeFileItemViewModel *viewModel) {
        @strongify(self);
        switch (viewModel.fileType) {
            case HYHomeFileType_File:
                {
                    [self showWordFile:viewModel.filePath];
                }
                break;
            case HYHomeFileType_Pic:
                {
                    [self showImagePath:@[viewModel.filePath] currentImageIndex:0];
                }
                break;
            case HYHomeFileType_Audio:
                {
                    [self showAudioWithItemVM:viewModel];
                }
                break;
            case HYHomeFileType_Video:
                {
                    [self showVideoWithItemVM:viewModel];
                }
                break;
            case HYHomeFileType_Other:
                {
                    [HYHub showMessage:NSLocalizedString(@"该文件暂不支持预览，技术小哥正在加快研发中", nil)];
                }
                break;
                
            default:
                break;
        }
    }];
}

- (void)loadData
{
    @weakify(self);
    [self.viewModel loadData];
    dispatch_async(dispatch_get_main_queue(), ^{
        @strongify(self);
        [self.tableView reloadData];
    });
}

- (void)configUI
{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.navigationBarView.mas_bottom);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(self.view.mas_bottom);
        }
    }];
}
#pragma mark - 查看文本
- (void)showWordFile:(NSString *)filePath
{
    HYWKWebViewController *webVC = [[HYWKWebViewController alloc]init];
    webVC.localUrl = filePath;
    [self.navigationController pushViewController:webVC animated:YES];
}
- (void)showImagePath:(NSArray *)imagePaths currentImageIndex:(NSInteger)currentIndex
{
    HYPreviewImageController *showImgVC = [[HYPreviewImageController alloc]initWithImages:imagePaths currentIndex:currentIndex];
    showImgVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:showImgVC animated:YES completion:nil];
}

#pragma mark - 查看音频
- (void)showAudioWithItemVM:(HYHomeFileItemViewModel *)itemVm
{
    HYVideoViewModel *viewModel = [self.viewModel fetchPlayVideoConfigWithItemViewModel:itemVm];
    HYAudioRoomViewController *audioRoomVC = [[HYAudioRoomViewController alloc]initWithViewModel:viewModel];
    [self.navigationController pushViewController:audioRoomVC animated:YES];
}

#pragma mark - 查看视频
- (void)showVideoWithItemVM:(HYHomeFileItemViewModel *)itemVm
{
    HYVideoViewModel *viewModel = [self.viewModel fetchPlayVideoConfigWithItemViewModel:itemVm];
    HYVideoRoomViewController *videoRoomVC = [[HYVideoRoomViewController alloc]initWithViewModel:viewModel];
    [self.navigationController pushViewController:videoRoomVC animated:YES];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.viewModel numberOfSectionsInTableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.viewModel numberOfRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1){
        HYHomeFileType fileType = [self.viewModel fetchFileType:indexPath];
        HYHomeBaseListCell *baseCell;
        if(fileType != HYHomeFileType_Video && fileType != HYHomeFileType_Audio){// 文件夹类型
            baseCell = [tableView dequeueReusableCellWithIdentifier:@"CELLIDHYHomeFolderListCell" forIndexPath:indexPath];
        }else{// 视频类型
            baseCell = [tableView dequeueReusableCellWithIdentifier:@"CELLIDHYHomeVideoListCell" forIndexPath:indexPath];
        }
        baseCell.setBtn.hidden = YES;
        [baseCell bindViewModel:[self.viewModel cellForRowAtIndexPath:indexPath]];
        return baseCell;
    }
    HYFaceToFaceCell *FaceCell = [tableView dequeueReusableCellWithIdentifier:@"CELLIDHYFaceToFaceCell" forIndexPath:indexPath];
    [FaceCell bindViewModel:[self.viewModel cellForRowAtIndexPath:indexPath]];
    return FaceCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.viewModel heightForRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.viewModel didSelectRowAtIndexPath:indexPath];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HYSettingHeadView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CELLIDHYSettingHeadView"];
    NSString *headString = [self.viewModel headerTitleForHeaderInSection:section];
    [headView setHeadTitle:headString withTextAlignment:NSTextAlignmentLeft];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
    return view;
}

#pragma mark getter
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = AppBgThemeColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[HYHomeFolderListCell class] forCellReuseIdentifier:@"CELLIDHYHomeFolderListCell"];
        [_tableView registerClass:[HYHomeVideoListCell class] forCellReuseIdentifier:@"CELLIDHYHomeVideoListCell"];
        [_tableView registerClass:[HYFaceToFaceCell class] forCellReuseIdentifier:@"CELLIDHYFaceToFaceCell"];
        [_tableView registerClass:[HYSettingHeadView class] forHeaderFooterViewReuseIdentifier:@"CELLIDHYSettingHeadView"];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}
- (HYFaceToFaceViewModel *)viewModel
{
    if (_viewModel == nil) {
        _viewModel = [[HYFaceToFaceViewModel alloc]init];
    }
    return _viewModel;
}
@end
